﻿using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using NoFake.IO.Exceptions;
using NoFake.IO.Helpers;


[assembly: InternalsVisibleTo("NoFake.IO.Tests")]

namespace NoFake.IO.Files
{
    internal class EmptyFile : NFile
    {
        internal EmptyFile(NDirectory directory, string fileName) : base(fileName)
        {
            Contract.Requires<ArgumentException>(directory != null, "IDirectory is null");


            try
            {
                Create(directory);
            }
            catch (Exception ex)
            {
                ThrowConstructorException(ex);
            }
        }


        private void Create(NDirectory directory)
        {
            Contract.Requires(directory != null);
            FullPath = Path.Combine(directory.FullPath, FileName);

            if (File.Exists(FullPath))
            {
                throw new Exception("file already exist");
            }

            IOManager.CreateEmptyFile(FullPath);
        }

        private void ThrowConstructorException(Exception ex)
        {
            Contract.Requires(ex != null);
            throw new FileTakeException(String.Format("TempFile '{0}' constructor exception: {1}", FileName, ex.Message), ex);
        }

        #region Dispose

        protected override void Disposing()
        {
            //Delete file
            if (FullPath != null && File.Exists(FullPath))
            {
                IOManager.DeleteFile(FullPath);
            }
        }

        #endregion
    }
}